﻿namespace TestExtensions.Reporting
{
    public static class TestReportExtensions
    {
        public static void Report(this TestResults @this, RenderTarget target)
        {
            @this.Report(ReportRendererFactory.GetRenderer(target));
        }

        public static void Report(this TestResults @this, IReportRenderer renderer)
        {
            renderer.WriteLine("Test: {0}", @this.TestPath);
            @this.Summary.Report(renderer);
            @this.ForEach(iteration => iteration.Report(renderer));
        }

        public static void Report(this Summary @this, IReportRenderer renderer)
        {
            renderer.WriteLine("Run started: {0}", @this.RunStarted);
            renderer.WriteLine("Run ended: {0}", @this.RunEnded);
            renderer.WriteNewLine();
            renderer.WriteLine("Status  \tTimes");
            renderer.WriteHorizontalRule();
            renderer.WriteLine("Passed  \t{0}", @this.Passed);
            renderer.WriteLine("Failed  \t{0}", @this.Failed);
            renderer.WriteLine("Warnings\t{0}", @this.Warnings);
        }

        public static void Report(this TestIteration @this, IReportRenderer renderer)
        {
            renderer.WriteNewLine();
            renderer.WriteLine("Iteration {0}: {1}", @this.IterationID, @this.Status);
            renderer.WriteLine(new string('-', 32));
            @this.ForEach(action => action.Report(renderer));
        }

        public static void Report(this TestAction @this, IReportRenderer renderer)
        {
            renderer.WriteNewLine();
            renderer.WriteLine("Action: {0}", @this.Name);
            @this.Summary.Report(renderer);
            @this.Step.Report(renderer);
        }

        public static void Report(this TestStep @this, IReportRenderer renderer, int level = 0)
        {
            var tabs = new string('\t', level);
            renderer.WriteLine("{0}Step: {1} - {2}", tabs, @this.Object, @this.Time);
            renderer.WriteLine("{0}{1}", tabs, @this.Details);
            @this.ForEach(subStep => subStep.Report(renderer, level + 1));
        }
    }
}